package com.huixue.cn.ztej.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.User;
import com.huixue.cn.ztej.constants.UserUtil;
import com.huixue.cn.ztej.enums.RoleStatus;
import com.huixue.cn.ztej.service.SysMechanicaApplyService;
import com.huixue.cn.ztej.service.SysUserService;
import com.huixue.cn.ztej.service.impl.EmployApplyServiceImpl;
import com.huixue.cn.ztej.service.impl.ExamineServiceImpl;
import com.huixue.cn.ztej.weixin.bean.OAuthInfo;
import com.huixue.cn.ztej.weixin.util.WeixinHelper;

/**
 * 信息统计
 * @author gongdongyang
 *
 */

@Controller
@RequestMapping(value = "/statistics")
public class WStatisticsController extends BaseController {
	
	@Autowired
	SysMechanicaApplyService sysMechanicaApplyService;
	
	@Autowired(required = true)
	SysUserService sysUserService;
	
	@Autowired
	private ExamineServiceImpl examineServiceImpl;
	
	@Autowired
	private EmployApplyServiceImpl employApplyServiceImpl;
	
	
	@RequestMapping(value = "/list")
	private String list(HttpServletRequest request,HttpServletResponse response,Model model){
		
		OAuthInfo aouAuthInfo  = WeixinHelper.getUserOpenId(request);
		String status = "-1";
		Map<String, Object> map = new HashMap<String, Object>();
		
		//根据Openid查找用户
		User currentUser = sysUserService.findUserByOpenId(aouAuthInfo.getOpenId(),false);
		
		if(currentUser!=null){
			request.getSession().setAttribute("currentUser",currentUser);
		}

		if(currentUser==null){
			return "/wx/user/noregister";
		}else if(currentUser.getIsAvailable()==UserUtil.DISABLE_YES){
			model.addAttribute("msg", "账号已禁用,请联系管理员");
			return "/wx/message";
		}
		
		if(currentUser.getRoleId()!=null && 
				Integer.valueOf(currentUser.getRoleId())==RoleStatus.ROLE_9.getRoleId()
				||Integer.valueOf(currentUser.getRoleId())==RoleStatus.ROLE_7.getRoleId()){
			
			//查询机械申请
			Results mechanResult = sysMechanicaApplyService.searchMechanicalApplyPage(map,1,80);
			model.addAttribute("mechanResult", mechanResult);
		
			
			//查询用工申请
			Results empoyResults = employApplyServiceImpl.searchByPage(map,0,80);
			model.addAttribute("empoyResults", empoyResults);
			
			model.addAttribute("user", currentUser);
			
		}else{
			model.addAttribute("msg", "您没有相关统计查询权限");
			return "/wx/message";
		}
		
		return "/wx/statistics/list";
	}
}
